/**
 * websocket in Utils
 * @author Rui Luo
 * @date 2010-8-1
 */
if (typeof Utils != 'object') {
	Utils = {};
}
Utils.websocket = function(wsurl, oCfg) {
	if ('WebSocket' in window && wsurl) {
		var me = arguments.callee;
		var ws = new window.WebSocket(wsurl);
		oCfg = oCfg || {};
		ws.onopen = oCfg.onopen || _doNothing;
		ws.onmessage = oCfg.onmessage || _doNothing;
		ws.onclose = oCfg.onclose || _doNothing;
		ws.onerror = oCfg.onerror || _doNothing;

		function _getState() {
			return ws.readyState;
		}

		function _getWebSocket(){
			return ws;
		}

		function _close(){
			ws.close();
		}

		function _send(data){
			ws.send(data);
		}
		
		function _doNothing(){

		}

		return {
			send: _send,
			close: _close,
			getState: _getState,
			getWebSocket: _getWebSocket,
			constructor: me
		}
	}else{
		return {
			toString : function(){return null}
		};
	}

};
